using IQIGame.Onigao.Framework;
using NetProtocol.POD;
using System;
using System.Collections.Generic;

namespace IQIGame.Onigao.GamePlay
{
    /// <summary>
    /// 关卡种Buff持有者，或者叫简易的buff管理器，使用组合方式嵌入到实体或者英雄或者player中。
    /// </summary>
    public class LevelBuffHolder : IDisposable
    {
        public BetterList<int, LevelBuffData> buffs { get; } = new BetterList<int, LevelBuffData>();
        public LevelBuffHostType hostType { get; private set; }
        public long hostId { get; private set; }

        public event Action<LevelBuffData> onBuffRemove;
        public event Action<LevelBuffData, bool, int> onBuffUpdate;

        public LevelBuffHolder(LevelBuffHostType hostType, long hostId)
        {
            this.hostType = hostType;
            this.hostId = hostId;
        }

        public void Init(List<LevelBuffPOD> buffPODs)
        {
            foreach (var pod in buffPODs)
            {
                var buffData = LevelBuffData.CreateLevelBuff(pod);
                buffs.Add(buffData.cid, buffData);
            }
        }

        /// <summary>
        /// 处理buff的修改
        /// </summary>
        /// <param name="changePod"></param>
        public void ProcessBuffChange(LevelBuffChangePOD changePod)
        {
            if (changePod.removeCid > 0)
            {
                // 移除buff
                if (buffs.TryGetValue(changePod.removeCid, out var buffData))
                {
                    buffs.Remove(changePod.removeCid);
                    DispatchBuffRemoveEvent(buffData);
                    ClassPool.Put(buffData);
                }
            }
            if (changePod.addOrChangePOD != null)
            {
                if (buffs.TryGetValue(changePod.addOrChangePOD.cid, out var existBuffData))
                {
                    // 更新
                    int oldStack = existBuffData.stack;
                    existBuffData.RefreshByPod(changePod.addOrChangePOD);
                    DispatchBuffUpdateEvent(existBuffData, false, existBuffData.stack - oldStack);
                }
                else
                {
                    // 创建
                    var buffData = LevelBuffData.CreateLevelBuff(changePod.addOrChangePOD);
                    buffs.Add(buffData.cid, buffData);
                    DispatchBuffUpdateEvent(existBuffData, true, 1);
                }
            }
        }

        private void DispatchBuffUpdateEvent(LevelBuffData buffData, bool isNew, int addStack)
        {
            //单独事件，场景可侦听该事件提高性能
            onBuffUpdate?.Invoke(buffData, true, addStack);
            //广播时间，用于UI等全局信息展示
            MsgDispatcher.Broadcast(MsgEventType.Level_UpdateBuff, buffData, isNew, hostType, hostId);
        }

        private void DispatchBuffRemoveEvent(LevelBuffData buffData)
        {
            //单独事件，场景可侦听该事件提高性能
            onBuffRemove?.Invoke(buffData);
            //广播时间，用于UI等全局信息展示
            MsgDispatcher.Broadcast(MsgEventType.Level_RemoveBuff, buffData, hostType, hostId);
        }

        public bool ContainsBuff(int buffCid)
        {
            return buffs.ContainsKey(buffCid);
        }

        public void Clear()
        {
            foreach (var buffData in buffs)
            {
                ClassPool.Put(buffData);
            }
            buffs.Clear();
            MsgDispatcher.Broadcast(MsgEventType.Level_RemoveAllBuff, hostType, hostId);
        }

        public void Dispose()
        {
            Clear();
            onBuffRemove = null;
            onBuffUpdate = null;
        }


    }


}
